package com.imti.mall.admin.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imti.mall.admin.mapper.UmsAdminMapper;
import com.imti.mall.admin.mapper.UmsRoleMapper;
import com.imti.mall.admin.service.UmsAdminCacheService;
import com.imti.mall.admin.service.UmsAdminService;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.common.Api.ResultCode;
import com.imti.mall.common.exception.BusinessException;
import com.imti.mall.common.utils.JwtTokenUtil;
import com.imti.mall.model.admin.query.UmsAdminLoginParam;
import com.imti.mall.model.admin.pojo.UmsAdmin;
import com.imti.mall.model.admin.pojo.UmsRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Slf4j
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin> implements UmsAdminService {

    @Autowired
    private UmsAdminMapper adminMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UmsRoleMapper umsRoleMapper ;

    @Override
    public CommonResult login(UmsAdminLoginParam umsAdminLoginParam) {

        //密码需要客户端加密后传递
        String token = null;

        //1.正常登录 用户名和密码
        try {
            if(StrUtil.isNotBlank(umsAdminLoginParam.getUsername()) && StrUtil.isNotBlank(umsAdminLoginParam.getPassword())){

                //1.2 比对密码
                UmsAdmin umsAdmin = adminMapper.selectOne(
                    new LambdaQueryWrapper<UmsAdmin>().eq(UmsAdmin::getUsername, umsAdminLoginParam.getUsername()));
                if(ObjectUtil.isNull(umsAdmin)){
                    return CommonResult.failed(ResultCode.VALIDATE_FAILED, "用户名输入错误！");
                } else if (!SecureUtil.md5(umsAdminLoginParam.getPassword()).equals(umsAdmin.getPassword())) {
                    return CommonResult.failed(ResultCode.VALIDATE_FAILED, "密码输入错误！");
                } else {

                    token = jwtTokenUtil.generateToken(umsAdmin.getUsername());
                    return CommonResult.success(token);
                }
            }else {
                //2.未输入信息
                return CommonResult.failed();
            }
        } catch (BusinessException e) {
            log.error("登录异常:{}", e.getMessage());
        }
        return CommonResult.failed();
    }

    @Override
    public UmsAdmin getCurrentAdmin() {

        log.debug("umsadmin >>>>> ", request);
//        String userStr = request.getHeader(AuthConstant.USER_TOKEN_HEADER);
//        if(StrUtil.isEmpty(userStr)){
//            Asserts.fail(ResultCode.UNAUTHORIZED);
//        }
//        String userStr = "admin";
//        UserDto userDto = JSONUtil.toBean(userStr, UserDto.class);
        UmsAdmin admin = getCacheService().getAdmin(3L);
        if(admin!=null){
            return admin;
        }else{
            admin = adminMapper.selectById(3L);
            getCacheService().setAdmin(admin);
            return admin;
        }
    }

    @Override
    public List<UmsRole> getRoleList(Long adminId) {
        return umsRoleMapper.getRoleList(adminId);
    }


    @Override
    public UmsAdminCacheService getCacheService() {
        return SpringUtil.getBean(UmsAdminCacheService.class);
    }
}
